﻿#include "threadpool.h"
#include <chrono>
#include <iostream>
#include <thread>

using namespace std;
using namespace std::chrono;
using ulong = unsigned long long;

class MyTask : public Task
{
public:
    MyTask()
    {

    }
    MyTask(int begin, int end)
        : begin_(begin)
        , end_(end)
    {
    }

    virtual Any run() override
    {
        std::cout << "begin threadFunc tid: " << 
            std::this_thread::get_id() << std::endl;

        //std::this_thread::sleep_for(std::chrono::seconds(3));
        ulong sum = 0;
        for (ulong i = begin_; i <= end_; ++i)
        {
            sum += i;
        }

        std::cout << "end threadFunc tid: " << 
            std::this_thread::get_id() << std::endl;

        return sum; 
    }

private:
    int begin_;
    int end_;
};


int main()
{
    {
        ThreadPool pool;
        pool.setMode(PoolMode::MODE_CACHED);
        pool.start(2);
        Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        //ulong sum1 = res1.get().cast_<ulong>();
        //cout << sum1 << endl;
    }
    cout << "main over" << endl;
    //getchar();
#if 0
    {
        ThreadPool pool;
        pool.setMode(PoolMode::MODE_CACHED);
        pool.start(1);

        // 并行计算时间测量
        auto parallel_start = high_resolution_clock::now();

        Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 1'0000'0000));
        Result res2 = pool.submitTask(std::make_shared<MyTask>(1'0000'0001, 2'0000'0000));
        Result res3 = pool.submitTask(std::make_shared<MyTask>(2'0000'0001, 3'0000'0000));
        //pool.submitTask(std::make_shared<MyTask>(1'0000'0001, 2'0000'0000));
        //
        //pool.submitTask(std::make_shared<MyTask>(2'0000'0001, 3'0000'0000));
        //pool.submitTask(std::make_shared<MyTask>(2'0000'0001, 3'0000'0000));

        ulong sum1 = res1.get().cast_<ulong>();
        ulong sum2 = res2.get().cast_<ulong>();
        ulong sum3 = res3.get().cast_<ulong>();

        ulong total_parallel = sum1 + sum2 + sum3;

        auto parallel_end = high_resolution_clock::now();
        auto parallel_duration = duration_cast<milliseconds>(parallel_end - parallel_start);

        cout << "并行计算结果: " << total_parallel << endl;
        cout << "并行计算耗时: " << parallel_duration.count() << " 毫秒" << endl << endl;
    }

    // 单线程串行计算时间测量
    auto serial_start = high_resolution_clock::now();

    ulong sum_serial = 0;
    for (int i = 1; i <= 3'0000'0000; ++i)
        sum_serial += i;

    auto serial_end = high_resolution_clock::now();
    auto serial_duration = duration_cast<milliseconds>(serial_end - serial_start);

    cout << "串行计算结果: " << sum_serial << endl;
    cout << "串行计算耗时: " << serial_duration.count() << " 毫秒" << endl;

    getchar();
#endif
    return 0;
}